package dto

import (
	"gorm.io/gorm"
	"project2/g"
	"project2/model"
	"project2/utils"
	"time"
)

type SysDictionaryValueDto struct {
	Id           uint      `json:"id"`
	Name         string    `json:"name"`
	Code         string    `json:"code"`
	DicId        uint      `json:"dicId"`
	State        uint      `json:"state"`
	OrderNum     uint      `json:"orderNum"`
	Remarks      string    `json:"remarks"`
	TenantType   uint      `json:"tenantType"`
	TenantId     uint      `json:"tenantId"`
	DelState     uint      `json:"delState"`
	AddUser      string    `json:"addUser"`
	AddUserId    uint      `json:"addUserId"`
	AddTime      time.Time `json:"addTime"`
	EditUser     string    `json:"editUser"`
	EditUserId   uint      `json:"editUserId"`
	LastEditTime time.Time `json:"lastEditTime"`
	ServerNumber uint      `json:"serverNumber"`
	DelTime      time.Time `json:"delTime"`
}

func BuildSysDictionaryValueDto(_body *map[string]interface{}, dto *SysDictionaryValueDto) {
	body := *_body
	if body == nil {
		return
	}

	if body["id"] != nil && utils.ParserInt8(body["id"]) > 0 {
		dto.Id = uint(utils.ParserInt8(body["id"]))
	}

	if body["name"] != nil && body["name"].(string) != "" {
		dto.Name = body["name"].(string)
	}

	if body["code"] != nil && body["code"].(string) != "" {
		dto.Code = body["code"].(string)
	}

	if body["dicId"] != nil && utils.ParserInt8(body["dicId"]) > 0 {
		dto.DicId = uint(utils.ParserInt8(body["dicId"]))
	}

	if body["state"] != nil && utils.ParserInt8(body["state"]) > 0 {
		dto.State = uint(utils.ParserInt8(body["state"]))
	}

	if body["orderNum"] != nil && utils.ParserInt8(body["orderNum"]) > 0 {
		dto.OrderNum = uint(utils.ParserInt8(body["orderNum"]))
	}

	if body["remarks"] != nil && body["remarks"].(string) != "" {
		dto.Remarks = body["remarks"].(string)
	}

	if body["tenantType"] != nil && utils.ParserInt8(body["tenantType"]) > 0 {
		dto.TenantType = uint(utils.ParserInt8(body["tenantType"]))
	}

	if body["tenantId"] != nil && utils.ParserInt8(body["tenantId"]) > 0 {
		dto.TenantId = uint(utils.ParserInt8(body["tenantId"]))
	}

	if body["delState"] != nil && utils.ParserInt8(body["delState"]) > 0 {
		dto.DelState = uint(utils.ParserInt8(body["delState"]))
	}

	if body["addUser"] != nil && body["addUser"].(string) != "" {
		dto.AddUser = body["addUser"].(string)
	}

	if body["addUserId"] != nil && utils.ParserInt8(body["addUserId"]) > 0 {
		dto.AddUserId = uint(utils.ParserInt8(body["addUserId"]))
	}

	if body["addTime"] != nil && !utils.Str2date(body["addTime"].(string)).IsZero() {
		dto.AddTime = utils.Str2date(body["addTime"].(string))
	}

	if body["editUser"] != nil && body["editUser"].(string) != "" {
		dto.EditUser = body["editUser"].(string)
	}

	if body["editUserId"] != nil && utils.ParserInt8(body["editUserId"]) > 0 {
		dto.EditUserId = uint(utils.ParserInt8(body["editUserId"]))
	}

	if body["lastEditTime"] != nil && !utils.Str2date(body["lastEditTime"].(string)).IsZero() {
		dto.LastEditTime = utils.Str2date(body["lastEditTime"].(string))
	}

	if body["serverNumber"] != nil && utils.ParserInt8(body["serverNumber"]) > 0 {
		dto.ServerNumber = uint(utils.ParserInt8(body["serverNumber"]))
	}

	if body["delTime"] != nil && !utils.Str2date(body["delTime"].(string)).IsZero() {
		dto.DelTime = utils.Str2date(body["delTime"].(string))
	}
}
func (self *SysDictionaryValueDto) BuildQuery(query **gorm.DB) {
	(*query) = g.Db.Model(&model.SysAccount{})
	(*query).Where("del_time is not null")

	if self.Id > 0 {
		(*query).Where("id =?", self.Id)
	}

	if self.Name != "" {
		(*query).Where("name =?", self.Name)
	}

	if self.Code != "" {
		(*query).Where("code =?", self.Code)
	}

	if self.DicId > 0 {
		(*query).Where("dic_id =?", self.DicId)
	}

	if self.State > 0 {
		(*query).Where("state =?", self.State)
	}

	if self.OrderNum > 0 {
		(*query).Where("order_num =?", self.OrderNum)
	}

	if self.Remarks != "" {
		(*query).Where("remarks =?", self.Remarks)
	}

	if self.TenantType > 0 {
		(*query).Where("tenant_type =?", self.TenantType)
	}

	if self.TenantId > 0 {
		(*query).Where("tenant_id =?", self.TenantId)
	}

	if self.DelState > 0 {
		(*query).Where("del_state =?", self.DelState)
	}

	if self.AddUser != "" {
		(*query).Where("add_user =?", self.AddUser)
	}

	if self.AddUserId > 0 {
		(*query).Where("add_user_id =?", self.AddUserId)
	}

	if !self.AddTime.IsZero() {
		(*query).Where("add_time =?", self.AddTime)
	}

	if self.EditUser != "" {
		(*query).Where("edit_user =?", self.EditUser)
	}

	if self.EditUserId > 0 {
		(*query).Where("edit_user_id =?", self.EditUserId)
	}

	if !self.LastEditTime.IsZero() {
		(*query).Where("last_edit_time =?", self.LastEditTime)
	}

	if self.ServerNumber > 0 {
		(*query).Where("server_number =?", self.ServerNumber)
	}

	if !self.DelTime.IsZero() {
		(*query).Where("del_time =?", self.DelTime)
	}

}
func (self *SysDictionaryValueDto) BuildEntityForAdd(entity *model.SysDictionaryValue) {

	if self.Id > 0 {
		entity.Id = self.Id
	} else {
		entity.Id = 0
	}

	if self.Name != "" {
		entity.Name = self.Name
	} else {
		entity.Name = ""
	}

	if self.Code != "" {
		entity.Code = self.Code
	} else {
		entity.Code = ""
	}

	if self.DicId > 0 {
		entity.DicId = self.DicId
	} else {
		entity.DicId = 0
	}

	if self.State > 0 {
		entity.State = self.State
	} else {
		entity.State = 0
	}

	if self.OrderNum > 0 {
		entity.OrderNum = self.OrderNum
	} else {
		entity.OrderNum = 0
	}

	if self.Remarks != "" {
		entity.Remarks = self.Remarks
	} else {
		entity.Remarks = ""
	}

	if self.TenantType > 0 {
		entity.TenantType = self.TenantType
	} else {
		entity.TenantType = 0
	}

	if self.TenantId > 0 {
		entity.TenantId = self.TenantId
	} else {
		entity.TenantId = 0
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	} else {
		entity.DelState = 0
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	} else {
		entity.AddUser = ""
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	} else {
		entity.AddUserId = 0
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	} else {
		entity.EditUser = ""
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	} else {
		entity.EditUserId = 0
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	} else {
		entity.ServerNumber = 0
	}

}
func (self *SysDictionaryValueDto) BuildEntityForUpdate(entity *model.SysDictionaryValue) {
	entity.Id = self.Id

	if self.Id > 0 {
		entity.Id = self.Id
	}

	if self.Name != "" {
		entity.Name = self.Name
	}

	if self.Code != "" {
		entity.Code = self.Code
	}

	if self.DicId > 0 {
		entity.DicId = self.DicId
	}

	if self.State > 0 {
		entity.State = self.State
	}

	if self.OrderNum > 0 {
		entity.OrderNum = self.OrderNum
	}

	if self.Remarks != "" {
		entity.Remarks = self.Remarks
	}

	if self.TenantType > 0 {
		entity.TenantType = self.TenantType
	}

	if self.TenantId > 0 {
		entity.TenantId = self.TenantId
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	}

}
